package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.exception.CustomException;
import cn.wolfcode.p2p.business.query.CreditTransferQuery;
import cn.wolfcode.p2p.business.service.ICreditTransferService;
import cn.wolfcode.p2p.util.JSONResult;
import cn.wolfcode.p2p.util.PageResult;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class CreditTransferController {
        @Autowired
        private ICreditTransferService creditTransferService;

        @RequestMapping("credit_transfer_list")
        public String creditTransferList(@ModelAttribute("qo")CreditTransferQuery qo, Model model){
                qo.setLoginInfoId(UserContext.getLoginInfo().getId());
                PageResult pageResult = creditTransferService.queryCanCreditTransfer(qo);
                model.addAttribute("pageResult",pageResult);
                return "credittransfer_cantrans_list";
        }
        @RequestMapping("creditTransfer")
        @ResponseBody
        public JSONResult creditTransfer(Long[] bidId){
                JSONResult jsonResult = new JSONResult();
                try {
                        creditTransferService.creditTransfer(bidId);
                    }catch (CustomException e ){
                        jsonResult.setMsg(e.getMessage());//自定义的异常,避免所有异常都返回同样的信息给客户
                    } catch (Exception e) {
                        e.printStackTrace();
                        jsonResult.setMsg("系统繁忙,请稍后再试");
                }
                 return jsonResult;
        }

        /**
         * 我的债权标
         */
        @RequestMapping("listMyTransferCredit")
        public String listMyTransferCredit(@ModelAttribute("qo")CreditTransferQuery qo,Model model){
                qo.setLoginInfoId(UserContext.getLoginInfo().getId());
                PageResult pageResult = creditTransferService.queryMyTransfer(qo);
                model.addAttribute("pageResult",pageResult);
                return "credittransfer_mytrans_list";
        }

        /**
         * 撤销债权标的方法,拿到债权标的ID,先修改对应的BIDID的所有状态为普通,然后删除该债权标,有空再做
         */

        @RequestMapping("subscribe")
        @ResponseBody
        public JSONResult subscribe(Long id){
                JSONResult jsonResult = new JSONResult();
                try {
                        creditTransferService.subscribe(id);
                    }catch (CustomException e ){
                        jsonResult.setMsg(e.getMessage());//自定义的异常,避免所有异常都返回同样的信息给客户
                    } catch (Exception e) {
                        e.printStackTrace();
                        jsonResult.setMsg("系统繁忙,请稍后再试");
                }
                 return jsonResult;
        }

}
